#include "Bot.h"
#include "Order.h"
#include "Strategy.hh"

using namespace std;


//constructor
Bot::Bot()
{

};

//plays a single game of Ants.
void Bot::playGame()
{
    //reads the game parameters and sets up
    cin >> state;
    state.setup();
    endTurn();

    //continues making moves while the game is not over
    while(cin >> state)
    {
        state.updateVisionInformation();
        makeMoves();
        endTurn();
    }
};

//makes the bots moves for the turn
void Bot::makeMoves()
{
    state.bug << "turn " << state.turn << ":" << endl;
    state.bug << state << fflush << endl;

    //update our knowledge of the map and recover all known places
    update_map_knowledge(state, map_knowledge);

    //set attractiveness of places based on known food and hills
    set_potentials(state);
    Order order;

    //picks out moves for each ant
    for(int ant=0; ant<(int)state.myAnts.size(); ant++)
    {
      // no move option
      order.proposeMove(state, state.myAnts[ant], state.myAnts[ant], -1, state.location(state.myAnts[ant]).value);

      for(int _d=0; _d<TDIRECTIONS; _d++)
        {
            // add a little randomness in direction order to avoid always favoring north
            int d=(_d+ant)%(TDIRECTIONS);
	  Location newloc = state.getLocation(state.myAnts[ant], d);
          if(!state.grid[newloc.row][newloc.col].isWater)
          {
	    double value = state.location(newloc).value;
	    order.proposeMove(state, state.myAnts[ant], newloc, d, value);
          }
	}
    }

    state.bug << "order size: " << order.size() << endl << endl;

#if 0
    Simulator sim(state);
    Order clone_orders(order);
    sim.simulate_one_step(clone_orders);
    if(sim.death!=0)
    {
        state.bug << "battle issue: " << sim.death << endl << endl;
    }
#endif

    order.execute(state);

    state.bug << "time taken: " << state.timer.getTime() << "ms" << endl << endl;
};

//finishes the turn
void Bot::endTurn()
{
    if(state.turn > 0)
        state.reset();
    state.turn++;

    cout << "go" << endl;
};
